package com.gxks.lhs.controller.admin;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gxks.lhs.constants.Constants;
import com.gxks.lhs.constants.R;
import com.gxks.lhs.entity.Goods;
import com.gxks.lhs.service.IGoodsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 商品表 前端控制器
 * </p>
 *
 * @author LuoHaisong
 * @since 2023-05-19
 */
@Api(tags = "商品管理模块")
@Slf4j
@RestController
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    private IGoodsService goodsService;

//    @ApiOperation("使用二手手机的id批量删除二手手机")
//    @DeleteMapping("/{ids}")
//    public R deleteByIds(String ids){
//        return goodsService.removeGoods(ids);
//    }

    @ApiOperation("根据二手手机的id删除二手手机")
    @DeleteMapping("/{id}")
    public R deleteById(@PathVariable(value = "id") String id) {
        boolean b = goodsService.removeGoodsById(id);
        if (b) {
            return R.success();
        }
        return R.error();
    }

    @ApiOperation("手机上下架")
    @PutMapping("/editStatus")
    public R editStatus(@RequestParam(value = "id") String id, @RequestParam(value = "status") Integer status) {
        boolean b = goodsService.updateGoodsStatus(id, status);
        if (b) {
            return R.success();
        }
        return R.error();
    }

    @ApiOperation("修改二手手机的信息")
    @PutMapping
    public R updateGoods(@RequestBody com.gxks.lhs.entity.Goods goods) {
        boolean b = goodsService.updateGoods(goods);
        if (b) {
            return R.success();
        }
        return R.error();
    }

    @ApiOperation("查询所有的二手手机")
    @GetMapping("/list")
    public R listGoods(
            @RequestParam(value = "current", defaultValue = "1") Integer current,
            @RequestParam(value = "size", defaultValue = "10") Integer size
    ) {
        Page<Goods> allGoods = goodsService.findAllGoods(current, size);
        if (allGoods.getTotal() > 0) {
            return R.success(allGoods, "查询成功");
        }
        return R.error("暂无数据");
    }

    @ApiOperation("根据条件查询二手手机的信息")
    @PostMapping("/find")
    public R findgoods(@RequestBody com.gxks.lhs.entity.Goods goods
    ) {
        Page<Goods> allGoods = goodsService.findGoods(goods, Constants.CURRENT_PAGE, Constants.PAGE_SIZE);
        if (allGoods.getTotal() > 0) {
            return R.success(allGoods, "查询成功");
        }
        return R.error("暂无数据");
    }

    @ApiOperation("根据id查找手机的详细信息")
    @GetMapping("/{id}")
    public R findById(@PathVariable(value = "id") String id) {
        Goods goods = goodsService.findGoodsById(id);
        if (goods != null) {
            return R.success(goods);
        }
        return R.error();
    }


}

